<template>
  <div>
    <basic-container>
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>推荐位管理</el-breadcrumb-item>
        <el-breadcrumb-item>书架</el-breadcrumb-item>
        <el-breadcrumb-item>书架推荐</el-breadcrumb-item>
      </el-breadcrumb>
    </basic-container>
    <basic-container>
      <avue-crud
        ref="crud"
        :data="data"
        v-model="form"
        :option="option"
        :page.sync="page"
        @on-load="getList"
        @refresh-change="refreshChange"
        @search-reset="from = {}"
        @search-change="searchChange"
        @row-update="rowUpdate"
        @row-save="rowSave"
        @row-del="rowDel"
      >
        <!--自定义表单 -->
        <template slot="messageForm">
          <el-tag v-if="message != ''" type="danger">{{ message }}</el-tag>
        </template>
        <template slot="last" slot-scope="scope">
          {{ scope.row.edit_time }}
          <el-tag>{{ scope.row.edit_admin_name }}</el-tag>
        </template>
        <template slot="menu" slot-scope="scope">
          <el-button
            icon="el-icon-edit"
            type="text"
            size="small"
            @click.stop="rowEdit(scope.row)"
          >
            <font>编辑</font>
          </el-button>
          <el-button
            icon="el-icon-info"
            type="text"
            size="small"
            @click.stop="childlist(scope)"
          >
            <font v-if="scope.row.card_type == 27">分类列表</font>
            <font v-else>书籍列表</font>
          </el-button>
        </template>
      </avue-crud>
    </basic-container>
  </div>
</template>


<script>
import { common } from "@/api/cat/httpRequest";
import {
  appConfigBookRecommend as baseURL,
  appConfigModule,
  recommendCardType,
} from "@/api/cat/baseUrls";
export default {
  data() {
    return {
      page: {
        total: 0,
        pageSize: 10,
      },
      cardTypeList: [],
      moduleList: [],
      type: true,
      data: [],
      params: {},
      form: {},
      message: "",
      default: {
        orderByList: "language&ASC,read_preference&ASC",
        is_customer: 2,
        m_id: "$in53",
      },
      option: {
        addBtn: false,
        border: true,
        selection: true,
        delBtn: false,
        editBtn: false,
        align: "center",
        headerAlign: "center",
        index: true,
        indexLabel: "编号",
        column: [
          {
            label: "语言",
            prop: "language",
            type: "select",
            search: true,
            placeholder: "请选择语言",
            addDisabled: true,
            editDisabled: true,
            dicData: [
              {
                label: "英语",
                value: 2,
              },
              {
                label: "芬兰语",
                value: 3,
              },
            ],
            props: {
              label: "label",
              value: "value",
            },
            rules: [
              {
                required: true,
                message: "语言必须填写",
              },
            ],
          },
          {
            label: "推荐位名称",
            prop: "m_name",
            type: "input",
            addDisplay: false,
            editDisplay: false,
            addDisabled: false,
            editDisabled: false,
            readonly: true,
          },
          {
            label: "阅读偏好",
            prop: "read_preference",
            type: "select",
            search: true,
            placeholder: "请填写阅读偏好",
            addDisabled: true,
            editDisabled: true,
            dicData: [
              {
                label: "男频",
                value: 1,
              },
              {
                label: "女频",
                value: 2,
              },
            ],
            props: {
              label: "label",
              value: "value",
            },
            rules: [
              {
                required: true,
                message: "偏好必须填写",
              },
            ],
          },
          {
            label: "用户属性",
            prop: "new_user",
            type: "select",
            search: true,
            placeholder: "请选择用户属性",
            dicData: [
              {
                label: "新用户",
                value: 0,
              },
              {
                label: "老用户",
                value: 1,
              },
            ],
            props: {
              label: "label",
              value: "value",
            },
          },
          {
            label: "书籍id配置",
            prop: "book_ids",
            type: "textarea",
            span: 24,
            placeholder: "请填写书籍id配置(多id间用英文,号隔开 或 一行一个)",
            rules: [
              {
                required: true,
                message: "书籍id配置必须填写",
              },
            ],
          },
          {
            label: "",
            prop: "message",
            type: "input",
            value: "",
            row: true,
            span: 24,
            hide: true,
            formslot: true,
          },
          {
            label: "最后操作信息",
            prop: "last",
            type: "input",
            addDisplay: false,
            editDisplay: false,
            addDisabled: false,
            editDisabled: false,
            readonly: true,
            slot: true,
          },
          {
            label: "是否自定义",
            prop: "is_customer",
            type: "input",
            value: 2,
            hide: true,
            addDisplay: false,
            editDisplay: false,
            addDisabled: false,
            editDisabled: false,
            readonly: true,
          },
          {
            label: "书籍类型",
            prop: "book_type",
            type: "input",
            value: 1,
            hide: true,
            addDisplay: false,
            editDisplay: false,
            addDisabled: false,
            editDisabled: false,
            readonly: true,
          },
        ],
      },
    };
  },
  methods: {
    /**获取列表 */
    async getList() {
      this.loading = true;
      //清空表格列表
      this.data = [];
      //发送请求获取数据
      let response = await common(
        `${baseURL.pageAndParam}`,
        { ...this.params, ...this.default },
        this.page,
        "POST"
      );
      this.page = response.page;
      response.data.forEach((e) => {
        e.book_ids = e.book_ids.join(",");
      });
      this.data = response.data;
      this.loading = false;
    },
    /**保存 */
    async rowSave(row, done, loading) {
      delete row["id"];

      let userInfo = this.$store.state.user.userInfo;
      row.edit_admin_name = userInfo.username;
      row.edit_admin_id = userInfo.id;
      row.book_ids = row.book_ids
        .replace(/\r\n|\n/g, ",")
        .split(",")
        .map((e) => e.trim());
      row.card_type = 19;
      let data = await common(`${baseURL.save}`, row, null, "POST");
      loading();
      if (data == null) {
        return;
      }

      this.getList();
      done();
    },
    /**修改 */
    async rowUpdate(row, index, done, loading) {
      let userInfo = this.$store.state.user.userInfo;
      row.edit_admin_name = userInfo.username;
      row.edit_admin_id = userInfo.id;
      row.book_ids = row.book_ids
        .replace(/\r\n|\n/g, ",")
        .split(",")
        .map((e) => e.trim());
      row.card_type = 19;
      let data = await common(`${baseURL.update}`, row, null, "POST");
      loading();
      if (data == null) {
        return;
      }

      this.getList();
      done();
    },
    /**刷新 */
    refreshChange() {
      this.getList();
    },
    /**删除 */
    rowDel(row) {
      this.$confirm("此操作将永久删除, 是否继续?", "提示", {
        type: "warning",
      })
        .then(async () => {
          let response = await common(
            `${baseURL.delete}`,
            { id: row.id },
            null,
            "GET"
          );
        })
        .then(() => {
          this.getList();
        });
    },
    async initCardTypeList() {
      let data = await common(
        recommendCardType.cardTypeList + "/0",
        null,
        null,
        "GET"
      );
      if (data.code == 1) {
        this.cardTypeList = data.data;
      }
    },
    async initModule() {
      let data = await common(
        `${appConfigModule.selectBookCommendModuleList}?type=-1`,
        null,
        null,
        "GET"
      );
      if (data.code == 1) {
        this.moduleList = data.data;
      }
    },
    /**编辑 */
    rowEdit(row) {
      let cardType;
      this.moduleList.forEach((e) => {
        if (e.m_id == 53) {
          cardType = e.m_card_type;
        }
      });
      let cardValue = this.cardTypeList.filter((e) => e.id == cardType)[0];
      if (cardValue) {
        this.message = "请最少配置" + cardValue.card_value + "本书";
      } else {
        this.message = "请最少配置10本书";
      }
      this.$refs.crud.rowEdit(row, row.$index);
    },
    /**搜索 */
    searchChange(params, done) {
      this.params = params;
      this.page.currentPage = 1;
      this.getList();
      if (done) done();
    },
    // 跳转
    childlist(scope) {
      this.$router.push({
        path: "/featured/bookinfo",
        query: {
          id: scope.row.id,
          preRouter: this.$route.path,
          name: this.$route.name,
        },
      });
    },
  },
  created() {
    this.initModule();
    this.initCardTypeList();
  },
};
</script>